import request from '@/core/request.js'
import Qs from 'qs'
import store from '@/store'
// import { system } from '@/core/config'

/**
 * 获取用户信息
 */
export function getInfo(token) {
  return request({
    url: '/api/uc/admin/info',
    method: 'get'
  })
}

/**
 * 退出登录
 */
export function logout() {
  return request({
    url: '/api/ac/auth/logout',
    method: 'post'
  })
}

/**
 * 登录接口
 * @param {*} username 用户名
 * @param {*} password 密码
 * @param {*} deviceId 设备id
 * @param {*} imageCode 验证码
 */
export function login(username, password) {
  return request({
    url: '/api/ac/auth/admin/login',
    method: 'post',
    transformRequest: [(data) => {
      data = Qs.stringify(data)
      return data
    }],
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      // 'Authorization': `Basic ${store.getters.getBasicToken}`
    },
    data: {
      username,
      password
    }
  })
}

/**
 * 获取图形验证码
 * @param {*} deviceId 设备id
 */
export function imageCode(deviceId) {
  return request({
    url: '/api/ac/auth/code/image',
    method: 'post',
    headers: {
      deviceId
    }
  })
}

export function refreshToken() {
  return request({
    url: '/api/ac/auth/user/refreshToken',
    method: 'post',
    headers: {
      'Authorization': `Basic ${store.getters.getBasicToken}`
    },
    // auth: {
    //   username: 'test-client',
    //   password: 'test-client'
    // },
    data: {
      refresh_token: store.getters.getRefreshToken,
      access_token: store.getters.getAccessToken
    }
  })
}
